package org.nlp2rdf.navigator.client;

import com.google.gwt.user.client.ui.*;
import org.nlp2rdf.navigator.client.common.LearnedConceptDTO;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;

//PopupPanel
public class AddLabelAndCommentDialogBox extends PopupPanel {
	private TextBox label;
	private TextBox comment;
	private final Button saveButton;
//	private final Application app;
//	private final PopupPanel glass;

	public AddLabelAndCommentDialogBox(final LearnedConceptDTO currentLearnedConcept, final Application app,
			final PopupPanel glass) {
		// this.app = app;
		// this.glass = glass;

		VerticalPanel popUpPanelContents = new VerticalPanel();

		Label title = new Label("Please enter a label, which describes your search collection best (required):");
		title.setStyleName("module-title");
		popUpPanelContents.add(title);

		HorizontalPanel lpanel = new HorizontalPanel();
        lpanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		lpanel.add(new Label("Label (required): "));
        lpanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		label = new TextBox();
		lpanel.add(label);
		popUpPanelContents.add(lpanel);

		HorizontalPanel cpanel = new HorizontalPanel();
        cpanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		cpanel.add(new Label("Comment (optional):"));
		comment = new TextBox();
        cpanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		cpanel.add(comment);
		popUpPanelContents.add(cpanel);


		HorizontalPanel buttonPanel = new HorizontalPanel();
		saveButton = new Button("save", new ClickHandler() {

			public void onClick(ClickEvent arg0) {
				currentLearnedConcept.setLabel(label.getText());
				currentLearnedConcept.setComment(comment.getText());
				app.getWorkerService().saveConcept(currentLearnedConcept, new AsyncCallback<Void>() {

					public void onSuccess(Void arg0) {

						app.notifyStoredConceptWidget();
						hide();
						glass.hide();

					}

					public void onFailure(Throwable arg0) {
						app.reportError(arg0);

					}
				});

			}
		});

		saveButton.setEnabled(false);

		buttonPanel.add(saveButton);

		Button cancelButton = new Button("cancel", new ClickHandler() {
			public void onClick(ClickEvent arg0) {
				hide();
				glass.hide();
			}
		});
		buttonPanel.add(cancelButton);

		KeyUpHandler k = new KeyUpHandler() {
			public void onKeyUp(KeyUpEvent event) {
				boolean check = check();
				saveButton.setEnabled(check);
			}
		};
		label.addKeyUpHandler(k);
		comment.addKeyUpHandler(k);

		popUpPanelContents.add(buttonPanel);
		PopupPanel p = new PopupPanel();
		p.setWidget(popUpPanelContents);
		// getGlass().show();

		setWidget(popUpPanelContents);
		// add(mainPopUpPanel);

	}

	private boolean check() {
		String test = label.getText();
		if (test.trim().length() == 0) {
			return false;
		}
		return true;
	}

}
